package com.ktjy.ruoyidata.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ktjy.ruoyidata.domain.ProProcessContent;
import com.ktjy.ruoyidata.mapper.ProProcessMapper;
import com.ktjy.ruoyidata.domain.ProProcess;
import com.ktjy.ruoyidata.service.IProProcessService;

/**
 * 生产工序Service业务层处理
 * 
 * @author t382
 * @date 2025-05-09
 */
@Service
public class ProProcessServiceImpl implements IProProcessService 
{
    @Autowired
    private ProProcessMapper proProcessMapper;

    /**
     * 查询生产工序
     * 
     * @param processId 生产工序主键
     * @return 生产工序
     */
    @Override
    public ProProcess selectProProcessByProcessId(Long processId)
    {
        return proProcessMapper.selectProProcessByProcessId(processId);
    }

    /**
     * 查询生产工序列表
     * 
     * @param proProcess 生产工序
     * @return 生产工序
     */
    @Override
    public List<ProProcess> selectProProcessList(ProProcess proProcess)
    {
        return proProcessMapper.selectProProcessList(proProcess);
    }

    /**
     * 新增生产工序
     * 
     * @param proProcess 生产工序
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProProcess(ProProcess proProcess)
    {
        // 生成供应商编码
        String prefix = "PROCESS"; // 默认
        String randomPart = String.format("%03d", new Random().nextInt(1000));

        String clientCode = prefix + randomPart;
        proProcess.setProcessCode(clientCode);
        proProcess.setCreateTime(DateUtils.getNowDate());
        int rows = proProcessMapper.insertProProcess(proProcess);
        insertProProcessContent(proProcess);
        return rows;
    }

    /**
     * 修改生产工序
     * 
     * @param proProcess 生产工序
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProProcess(ProProcess proProcess)
    {
        proProcess.setUpdateTime(DateUtils.getNowDate());
        proProcessMapper.deleteProProcessContentByProcessId(proProcess.getProcessId());
        insertProProcessContent(proProcess);
        return proProcessMapper.updateProProcess(proProcess);
    }

    /**
     * 批量删除生产工序
     * 
     * @param processIds 需要删除的生产工序主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProProcessByProcessIds(Long[] processIds)
    {
        proProcessMapper.deleteProProcessContentByProcessIds(processIds);
        return proProcessMapper.deleteProProcessByProcessIds(processIds);
    }

    /**
     * 删除生产工序信息
     * 
     * @param processId 生产工序主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProProcessByProcessId(Long processId)
    {
        proProcessMapper.deleteProProcessContentByProcessId(processId);
        return proProcessMapper.deleteProProcessByProcessId(processId);
    }

    /**
     * 新增生产工序内容信息
     * 
     * @param proProcess 生产工序对象
     */
    public void insertProProcessContent(ProProcess proProcess)
    {
        List<ProProcessContent> proProcessContentList = proProcess.getProProcessContentList();
        Long processId = proProcess.getProcessId();
        if (StringUtils.isNotNull(proProcessContentList))
        {
            List<ProProcessContent> list = new ArrayList<ProProcessContent>();
            for (ProProcessContent proProcessContent : proProcessContentList)
            {
                proProcessContent.setProcessId(processId);
                list.add(proProcessContent);
            }
            if (list.size() > 0)
            {
                proProcessMapper.batchProProcessContent(list);
            }
        }
    }
}
